package com.example.boosttestandroid.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.boosttestandroid.R;

public class CommonTitleBar extends LinearLayout {

    private TextView titleView;
    private ImageView leftView, rightView;
    private View left, right;
    private OnclickListener mOnClickListener;

    public CommonTitleBar(Context context) {
        super(context);
        initView(context);
        initListener();
    }

    public CommonTitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initListener();
    }

    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_common_titlebar, this);
        titleView = findViewById(R.id.tv_title_titlebar);
        left = findViewById(R.id.left_layout);
        right= findViewById(R.id.ringht_layout);
        leftView = findViewById(R.id.ima_left_titlebar);
        rightView = findViewById(R.id.ima_right_titlebar);
    }

    /**
     *  设置左边按钮资源
     *
     * @param src
     * @return
     */
    public CommonTitleBar setLeftView(int src) {
        leftView.setVisibility(View.VISIBLE);
        leftView.setImageResource(src);
        return this;
    }

    /*
     * 隐藏左边按钮
     * */
    public void hideLeftView() {
        leftView.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置右边按钮资源
     *
     * @param src
     * @return
     */
    public CommonTitleBar setRightView(int src) {
        rightView.setVisibility(View.VISIBLE);
        rightView.setImageResource(src);
        return this;
    }

    /**
     *  设置标题
     *
     * @param str
     * @return
     */
    public CommonTitleBar setTitleView(String str) {
        titleView.setText(str);
        return this;
    }

    private void initListener() {
        right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.clickRight();
                }
            }
        });

        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.clickLeft();
                }
            }
        });
    }

    public interface OnclickListener {
        void clickLeft();
        void clickRight();
    }

    public void setOnClickListener(OnclickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }
}
